interface ListItem {
  perms: string;
  children?: ListItem[];
  [key: string]: any; // 允许其他属性
}

type ListType = ListItem[];

export const filterKeyListbyKey = (key?: string, list?: ListType): string[] => {
  if (!list || !key) return [];

  let result: string[] = [];

  const findPath = (items: ListType, path: string[]): boolean => {
    for (const item of items) {
      const newPath = [...path, item.perms];

      if (item.perms === key) {
        result = newPath;
        return true;
      }

      if (item.children) {
        if (findPath(item.children, newPath)) {
          return true;
        }
      }
    }
    return false;
  };

  findPath(list, []);

  return result;
};

export function findAllParentsAndSelf(data: any[], keys: string[]) {
  const result: string[] = [];

  keys.forEach((key) => {
    const cur = filterKeyListbyKey(key, data);
    result.push(...cur);
  });

  const resultSet = [...new Set(result)];

  return resultSet;
}
